package com.weipai.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.base.PictureShowMapper;
import com.weipai.demo.model.PictureShow;
import com.weipai.demo.service.PictureService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/28 14:42
 * @Version 1.0
 */
@Service
public class PictureServiceImpl implements PictureService {

    private static final Logger log = LoggerFactory.getLogger(PictureServiceImpl.class);

    @Autowired
    private PictureShowMapper pictureShowMapper;




    //保存图片
    @Override
    public Response savePicture(PictureShow pictureShow) {
        Response response = new Response();
        if(pictureShow.getId()==null){
            pictureShowMapper.insert(pictureShow);
            response.setCode(RetCode.SUCCESS);

        }else {
           // int i1 = pictureShowMapper.updateByPrimaryKey(pictureShow);
            int i = pictureShowMapper.updateByPrimaryKeySelective(pictureShow);
            if (i>0){
                response.setCode(RetCode.UPDATESUCCESS);
            }else {
                response.setCode(RetCode.UPDATEERROR);
                return response;
            }
        }
        return response;
    }

    //分页展示图片
    @Override
    public LayuiTablePagingResult pictureShow(Integer page,Integer size,String pictureClass) {
        LayuiTablePagingResult result= null;
        try {
            PageHelper.startPage(page,size);

            PictureShow pictureShow=new PictureShow();
            pictureShow.setPictureClass(pictureClass);

            List<PictureShow> banners = pictureShowMapper.select(pictureShow);
            //List<JobNews> banners = jobNewsMapper.select(jobNews);

            PageInfo<PictureShow> pageInfo=new PageInfo<>(banners);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return result;
    }
}
